package com.yanxu.lock.upanddown;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/23-2025
 * @Description: 可以进行降级
 */
public class AllowDown {
    static ReentrantReadWriteLock reentrantReadWriteLock=new ReentrantReadWriteLock();
    static ReentrantReadWriteLock.ReadLock readLock=reentrantReadWriteLock.readLock();
    static ReentrantReadWriteLock.WriteLock writeLock=reentrantReadWriteLock.writeLock();

    public static void main(String[] args) {
        writeLock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+" 成功获取到了写锁!");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+" 尝试进行降级!");
            readLock.lock();
            System.out.println(Thread.currentThread().getName()+" 降级成功，获取到了读锁!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
            writeLock.unlock();
        }
    }
};